import { getGoods, listGoods } from "@/api/service/goods/goods";
import { Goods } from "@/api/service/goods/types";

import { goodsForm, productDraw } from "./form";

interface GoodsData {
  data: Goods[];
  selectedKeys: string[];
  current: Goods | undefined;
}

export const loading = ref(false);
export const goodsData = ref<GoodsData>({
  data: [],
  selectedKeys: [],
  current: undefined
});
let queryObj = {
  keyword: "",
  orderByColumn: "goods_id",
  isAsc: "desc",
  barCode: ""
};

export const goodsQuery = useQuery({
  ...queryObj
});
export const goodsKeys = ref<string[]>([]);

export const loadGoods = async () => {
  loading.value = true;
  const data = await listGoods(goodsQuery.value);
  goodsData.value.data = data.rows;
  goodsQuery.value.total = data.total;
  loading.value = false;
};
export const editProduct = async (id: string) => {
  productDraw.value = true;
  const data = await getGoods(id);
  goodsForm.value = data.data;
};
export const cellClick = (data: any) => {
  goodsData.value.current = data;
};
export const search = () => {
  goodsQuery.value.pageNum = 1;
  loadGoods();
};

export const resetQuery = () => {
  goodsQuery.value = useQuery({
    ...queryObj
  }).value;
};
